package ssi.cameraTest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Region;
import android.view.MotionEvent;
import android.view.View;

public class PalloccoView extends View {
	private float x;
	private float y;
	private float scale = 1.0f;
	private int x_length = 33;
	private int y_length = 25;
	private String author;
	private String subject;
	private boolean touched = false;
	private Paint myPaint;
	private Paint textPaint;
	private int alpha = 98;
	private int red = 57;
	private int green = 231;
	private int blue = 10;
	private float left_value;
	private float right_value;
	private float top_value;
	private float bottom_value;
	private CameraTest activity;

	public void setActivity(CameraTest activity) {
		this.activity = activity;
	}

	public PalloccoView(Context context) {
		super(context);
		myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		myPaint.setARGB(alpha, red, green, blue);
		myPaint.setAntiAlias(true);
		textPaint = new Paint(Paint.LINEAR_TEXT_FLAG);
		textPaint.setColor(Color.WHITE);
		textPaint.setTextSize(5.0f);
		textPaint.setAntiAlias(true);
	}
	
	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public float getScale() {
		return scale;
	}

	public void setScale(float scale) {
		this.scale = scale;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public float getX_length() {
		return x_length;
	}

	public float getY_length() {
		return y_length;
	}

	public boolean isTouched() {
		return touched;
	}

	public void touch() {
		this.touched = !touched;
	}

	@Override
	protected void onDraw(Canvas c) {
		left_value = x - x_length;
		right_value = x + x_length;
		top_value = y - y_length;
		bottom_value = y + y_length;
		c.drawRoundRect(new RectF(left_value, top_value, right_value, bottom_value), 5.0f, 4.0f, myPaint);
		if (touched) {
			c.drawText(author, left_value + 1.0f, top_value + 8.0f, textPaint);
			c.drawText(subject, left_value + 1.0f, top_value + 18.0f, textPaint);
		}
	}

	public void setLocation(float f, float g, float h) {
		x = f;
		y = g;
		scale = h;
		invalidate();
	}
	
	public boolean touchEvent(MotionEvent event) {
		int action = event.getAction();
		
        if (action == MotionEvent.ACTION_DOWN) {
        	if (!touched) {
        		System.out.println("LEFT VALUE IN TOUCH: " + left_value);
        		if ((new Region((int)left_value, (int)top_value, 
        				(int)right_value, (int)bottom_value)).
        				contains((int)event.getX(), (int)event.getY())) 
        		{
	        		System.out.println("CLICKED PALLOCCO");
	        		activity.scaleUp(this);
	        		return true;
	        	}
        	} else {
        		int left = (int) (x - x_length*scale);
        		int right = (int) (x + x_length*scale);
        		int top = (int) (y - y_length*scale);
        		int bottom = (int) (y + y_length*scale);
        		System.out.println("LEFT VALUE IN RETOUCH: " + left);
        		if ((new Region((int)left, (int)top, 
        				(int)right, (int)bottom)).
        				contains((int)event.getX(), (int)event.getY())) 
        		{
        			System.out.println("RECLICKED PALLOCCO");
        		}
        	}
        }
		return false;
	}
}
